package com.ktgj.service.service.impl;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.TimeUnit;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.ktgj.common.core.annotation.Excel;
import com.ktgj.common.core.constant.BusinessTypeConstants;
import com.ktgj.common.core.constant.CustomerConstants;
import com.ktgj.common.core.constant.VipHallConstants;
import com.ktgj.common.core.domain.R;
import com.ktgj.common.core.enums.EmHelper;
import com.ktgj.common.core.enums.TrainOrderEnum;
import com.ktgj.common.core.utils.DateUtils;
import com.ktgj.common.core.utils.SnowFlake;
import com.ktgj.common.core.utils.SnowflakeUtils;
import com.ktgj.common.redis.service.RedisService;
import com.ktgj.customer.api.RemoteCustomerService;
import com.ktgj.customer.api.domain.*;
import com.ktgj.flight.api.RemoteFlightService;
import com.ktgj.flight.api.domain.AirportTerminalApi;
import com.ktgj.flight.api.domain.TicketOrderApi;
import com.ktgj.pay.api.RemotePayService;
import com.ktgj.pay.api.domain.PayTypeApi;
import com.ktgj.service.domain.*;
import com.ktgj.service.domain.vo.*;
import com.ktgj.service.mapper.*;
import com.ktgj.service.service.IContractLinkViphallPriceService;
import com.ktgj.service.service.IVipchannelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ktgj.service.service.IOrderVipchannelService;

import javax.servlet.http.HttpServletRequest;

/**
 * VIP通道订单
Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-09-03
 */
@Service
public class OrderVipchannelServiceImpl implements IOrderVipchannelService 
{
    @Autowired
    private OrderVipchannelMapper  orderVipchannelMapper;
    @Autowired
    private RemoteCustomerService remoteCustomerService;
    @Autowired
    private IVipchannelService vipchannelService;
    @Autowired
    private OrderitemVipchannelMapper orderitemVipchannelMapper;
    @Autowired
    private RedisService redisService;
    @Autowired
    private RemoteFlightService flightService;
    @Autowired
    private IContractLinkViphallPriceService contractLinkViphallPriceService;
    @Autowired
    private RemoteFlightService remoteFlightService;
    @Autowired
    private OrderViphallMapper orderViphallMapper;
    @Autowired
    private ViphallMapper viphallMapper;
    @Autowired
    private VipchannelMapper vipchannelMapper;
    @Autowired
    private OrderBoardingMapper orderBoardingMapper;
    @Autowired
    private VipboardingMapper vipboardingMapper;
    @Autowired
    private RemotePayService payService;
    @Autowired
    private OrderOperationlogMapper orderOperationlogMapper;

    /**
     * 查询VIP通道订单

     * 
     * @param ordervipchannelId VIP通道订单
ID
     * @return VIP通道订单

     */
    @Override
    public OrderVipchannel selectOrderVipchannelById(Long ordervipchannelId)
    {
        return orderVipchannelMapper.selectOrderVipchannelById(ordervipchannelId);
    }

    /**
     * 查询VIP通道订单
列表
     * 
     * @param orderVipchannel VIP通道订单

     * @return VIP通道订单

     */
    @Override
    public List<OrderVipchannel> selectOrderVipchannelList(OrderVipchannel orderVipchannel)
    {
        return orderVipchannelMapper.selectOrderVipchannelList(orderVipchannel);
    }

    /**
     * 新增VIP通道订单

     * 
     * @param orderVipchannel VIP通道订单

     * @return 结果
     */
    @Override
    public int insertOrderVipchannel(OrderVipchannel orderVipchannel)
    {
        orderVipchannel.setCreateTime(DateUtils.getNowDate());
        return orderVipchannelMapper.insertOrderVipchannel(orderVipchannel);
    }

    /**
     * 修改VIP通道订单

     * 
     * @param orderVipchannel VIP通道订单

     * @return 结果
     */
    @Override
    public int updateOrderVipchannel(OrderVipchannel orderVipchannel)
    {
        orderVipchannel.setUpdateTime(DateUtils.getNowDate());
        return orderVipchannelMapper.updateOrderVipchannel(orderVipchannel);
    }

    /**
     * 批量删除VIP通道订单

     * 
     * @param ordervipchannelIds 需要删除的VIP通道订单
ID
     * @return 结果
     */
    @Override
    public int deleteOrderVipchannelByIds(Long[] ordervipchannelIds)
    {
        return orderVipchannelMapper.deleteOrderVipchannelByIds(ordervipchannelIds);
    }

    /**
     * 删除VIP通道订单
信息
     * 
     * @param ordervipchannelId VIP通道订单
ID
     * @return 结果
     */
    @Override
    public int deleteOrderVipchannelById(Long ordervipchannelId)
    {
        return orderVipchannelMapper.deleteOrderVipchannelById(ordervipchannelId);
    }

    /**
     *
     * @param ordervipchannelId
     * @return
     */
    @Override
    public List<OrderVipchannel> select(Long ordervipchannelId) {
        return orderVipchannelMapper.select(ordervipchannelId);
    }

    /**
     * VIP通道填写订单
     * @param token
     * @param request
     * @return
     */
    @Override
    public VipchannelFillOrder serviceInformation(String token, FillOrderRequest request) {
        Vipchannel vipchannel = vipchannelService.queryVipchannelByVipchannelId(request.getVipchannelId());
        R<Customer> customer = remoteCustomerService.getLoginCustomerByToken(token);
        StringBuilder builder;
        R<AirportTerminalApi> terminal = flightService.getAirportTerminal(vipchannel.getAirportTerminalId());

        VipchannelFillOrder fillOrder = new VipchannelFillOrder();
        fillOrder.setVipchannelId(vipchannel.getVipchannelId());
        fillOrder.setVipchannelName(vipchannel.getVipchannelName());
        fillOrder.setAddress(vipchannel.getAddress());
        if (terminal.getCode() == 200 && terminal.getData() != null)
            fillOrder.setStartTerminal(terminal.getData().getStartTerminal());
        String businessBeginHour = vipchannel.getBusinessBeginHour().toString();
        if (businessBeginHour != null) {
            builder = new StringBuilder(businessBeginHour);
            builder.insert(builder.length() - 2, ":");
            fillOrder.setBusinessBeginHour(builder.toString());
        }
        String businessEndHour = vipchannel.getBusinessEndHour().toString();
        if (businessEndHour != null) {
            builder = new StringBuilder(businessEndHour);
            builder.insert(builder.length() - 2, ":");
            fillOrder.setBusinessEndHour(builder.toString());
        }
        fillOrder.setIconPath(vipchannel.getIconPath());
        fillOrder.setViphallSetOutType(vipchannel.getViphallSetOutType());
        fillOrder.setFreeMember(vipchannel.getFreeMember());
        fillOrder.setTagName(vipchannel.getTagName());
        BigDecimal price = null;
        for (ContractLinkVipchannelPrice contractLinkVipchannelPrice : vipchannel.getLstVipchannelPrice()) {
            fillOrder.setBasePrice(contractLinkVipchannelPrice.getBasePrice());
            fillOrder.setRecommendedRetailprice(contractLinkVipchannelPrice.getRecommendedRetailprice());
            fillOrder.setSalePrice(contractLinkVipchannelPrice.getSalePrice());
            fillOrder.setAccompanyingPrice(contractLinkVipchannelPrice.getAccompanyingPrice());
            price = contractLinkVipchannelPrice.getSalePrice();
        }
        String airportId = vipchannel.getAirportCode();
        String airPortName = flightService.queryAirPortName(airportId);
        fillOrder.setAirPortName(airPortName);
        fillOrder.setContactName(request.getContactName());
        fillOrder.setContactPhone(request.getContactPhone());
        fillOrder.setArrivalDate(request.getArrivalDate());
        fillOrder.setPeopleNumber(request.getPeopleNumber());
        fillOrder.setCustomerName(customer.getData().getCustomerName());
        fillOrder.setContractPhoneOne(customer.getData().getContractPhoneOne());
        fillOrder.setContractPhoneTow(customer.getData().getContractPhoneTow());
        R<Long> depb = remoteCustomerService.queyUserDepositBusinessByToken(token);
        Long depositBusiness = depb.getData();
        fillOrder.setDepositBusiness(depositBusiness);
        R<Long> integralVoR = remoteCustomerService.queyUserCreditsNumByToken(token);
        Long points = integralVoR.getData();
        fillOrder.setPoints(points);
        CouponDto data = remoteCustomerService.queyUserCouponByToken(token, price).getData();
        fillOrder.setCoupon(data);
        return fillOrder;
    }

    /**
     * VIP通道订单生成
     *
     * @param token     请求头
     * @param vipchannelId   贵宾厅id
     * @param paymentAmount     单价
     * @param points      积分
     * @param couponId    优惠卷id
     * @param totalsalePrice 订单总金额
     * @param usedDeposit 预留款
     * @param contactName 联系人姓名
     * @return
     */
    @Override
    public String saveOrder(String token, Long vipchannelId, String arrivalDate, Long peopleNumber, BigDecimal paymentAmount, BigDecimal points, Long couponId, BigDecimal totalsalePrice, BigDecimal usedDeposit, String contactName, String contactPhone, String usePoint, String useDeposits) throws Exception {

        CalculationVOApi calculationVOApi = new CalculationVOApi();
        List<BigDecimal> pricesList = new ArrayList<>();
        BigDecimal prices = paymentAmount;
        pricesList.add(prices);
        calculationVOApi.setPrices(pricesList);
        calculationVOApi.setAmount(Math.toIntExact(peopleNumber));
        calculationVOApi.setCouponId(couponId);
        calculationVOApi.setDepositAmount(usedDeposit);
        if (useDeposits.equals("false")){
            calculationVOApi.setUseDeposit(false);
        }
        else if (useDeposits.equals("true")){
            calculationVOApi.setUseDeposit(true);
        }
        calculationVOApi.setPointAmount(Integer.parseInt(points.toString()));
        if (usePoint.equals("false")){
            calculationVOApi.setUsePoint(false);
        }
        else if (usePoint.equals("true")){
            calculationVOApi.setUsePoint(true);
        }
        CalculationDTO calculationDTO = remoteCustomerService.calculationRemotely(calculationVOApi);
        if (calculationDTO.getPrice() == null){
            throw new Exception("价格计算异常");
        }
        if (calculationDTO.getPrice().compareTo(totalsalePrice) != 0){
            throw new Exception("支付金额发生变化");
        }

        R<Customer> loginCustomerByToken = remoteCustomerService.getLoginCustomerByToken(token);
        Vipchannel vipchannel = vipchannelService.selectVipchannelById(vipchannelId);

        OrderVipchannel orderVipchannel = new OrderVipchannel();
        OrderitemVipchannel orderitemVipchannel = new OrderitemVipchannel();
        String orderSourceDesc = "购买:" + vipchannel.getVipchannelName();
        String orderType = VipHallConstants.newVip.getValue();
        List<CustomerCard> customerCards = remoteCustomerService.queryCardIdByCustomerId(loginCustomerByToken.getData().getCustomerId());

        for (com.ktgj.customer.api.domain.CustomerCard customerCard : customerCards) {
            if (customerCard.getCardId() != null && customerCard.getCardId() != 0){
                Map<String, Object> map = new HashMap<>();
                map.put("cardId", customerCard.getCardId());
                Card card = remoteCustomerService.cardInfo(map);
                if (card != null){
                    customerCard.setCardLevel(card.getCardLevel());
                }
                else {
                    throw new Exception("无效的会员卡");
                }
            }
            else {
                throw new Exception("会员卡没有关联卡id");
            }
        }

        customerCards.sort((y, x) -> Double.compare(y.getCardLevel(), x.getCardLevel()));
        com.ktgj.customer.api.domain.CustomerCard customerCardR = customerCards.get(0);

        String cardId = customerCardR.getCardCode();

        SnowFlake snowFlake = new SnowFlake(9, 9);
        //生成订单
        orderVipchannel.setVipchannelId(vipchannelId);
        orderVipchannel.setOrderNumber(String.valueOf(snowFlake.nextId()));
        orderVipchannel.setCardNumber(cardId);
        orderVipchannel.setCustomerId(loginCustomerByToken.getData().getCustomerId());
        orderVipchannel.setContractVipchannelid(vipchannel.getVipchannelId());
        Date date = DateUtils.getNowDate();
        orderVipchannel.setCreateTime(new Date());
        orderVipchannel.setContactName(contactName);
        orderVipchannel.setContactPhone(contactPhone);
        orderVipchannel.setStatus(CustomerConstants.waitPay.getValue());
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        try {
            orderVipchannel.setArrivalterminalTime(format.parse(arrivalDate));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        orderVipchannel.setPeopleNumber(peopleNumber);
        orderVipchannel.setPaymentAmount(new BigDecimal(0.1));
        orderVipchannel.setPoints(points);
        orderVipchannel.setDepositAmount(usedDeposit);
        orderVipchannel.setOpemployeeId(1L);
        orderVipchannel.setTotalSaleprice(paymentAmount.multiply(BigDecimal.valueOf(peopleNumber)));
        if (couponId != null && couponId != 0) {
            orderVipchannel.setCouponNum(String.valueOf(couponId));

            R<CouponBO> coupon = remoteCustomerService.couponByCustomerRightDetailId(couponId);
            if (coupon.getCode() == 200 && coupon.getData() != null) {
                if (coupon.getData().getCouponType().equals("CouponTypeFullMinus") || coupon.getData().getCouponType().equals("CouponTypeCashCoupon")) {
                    orderVipchannel.setCouponAmount(coupon.getData().getCouponPrice());
                } else {
                    BigDecimal price = coupon.getData().getCouponDiscount();
                    BigDecimal divide = price.divide(new BigDecimal(100));
                    BigDecimal countPrice = totalsalePrice.multiply(divide);
                    BigDecimal couponAmount = totalsalePrice.subtract(countPrice);
                    orderVipchannel.setCouponAmount(couponAmount);
                }
            }
        }
        orderVipchannel.setPaymentStatus(CustomerConstants.waitPay.getValue());
        orderVipchannel.setCreateTime(date);
        orderVipchannel.setUpdateTime(date);
        orderVipchannel.setSourceWayId(1L); //1：App
        orderVipchannelMapper.insertOrderVipchannel(orderVipchannel);

        for (int i = 0; i < peopleNumber.intValue(); i++) {
            //生成子订单
            orderitemVipchannel.setOrdervipchannelId(orderVipchannel.getOrdervipchannelId());
            orderitemVipchannel.setStatus(CustomerConstants.waitPay.getValue());
            orderitemVipchannel.setOpemployeeId(1L);
            orderitemVipchannel.setCreateTime(date);
            orderitemVipchannel.setPoints(points.longValue());
            orderitemVipchannel.setPaymentAmount(totalsalePrice.divide(BigDecimal.valueOf(peopleNumber)));
            orderitemVipchannelMapper.insertOrderitemVipchannel(orderitemVipchannel);
        }
        List<OrderitemVipchannel> list = new ArrayList<>();
        list.add(orderitemVipchannel);
        orderVipchannel.setOrderitemVipchannels(list);
        Long employeeId = 1L;
        if ((points != null && points.longValue() > 0L) || couponId != null || (usedDeposit != null && usedDeposit.longValue() > 0L)) {
            remoteCustomerService.spend(loginCustomerByToken.getData().getCustomerId(), points.longValue(), orderType, orderVipchannel.getOrdervipchannelId(), orderSourceDesc, couponId, usedDeposit, employeeId);
        }
        redisService.setCacheObject("ordervipchannel" + loginCustomerByToken.getData().getCustomerId() + "o" + orderVipchannel.getOrderNumber(), JSON.toJSON(orderVipchannel), 60 * 30L, TimeUnit.SECONDS);
        Map<String, String> map = new HashMap<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String orderDate = sdf.format(orderVipchannel.getCreateTime());
        //从哪获取订单信息

        String star = "您在"+orderDate + orderSourceDesc +"请在规定时间内支付";
        map.put("messageContent", star);
        //订单类型
        map.put("businessType",EmHelper.businessType.VIPAisle.getValue());
        map.put("customerId",loginCustomerByToken.getData().getCustomerId().toString());
        map.put("businessOrderId",orderVipchannel.getOrderNumber().toString());
        remoteCustomerService.addCustomerMessage(map);
        return orderVipchannel.getOrderNumber();
    }

    /**
     * VIP通道订单收银台
     * @param customerId
     * @return
     */
    @Override
    public VipchannelCashier queryByPayOrder(Long customerId, Long orderNumber, String businessType) throws Exception {
        VipchannelCashier cashier = new VipchannelCashier();
        Long expireTime = redisService.redisTemplate.getExpire("ordervipchannel" + customerId + "o" + orderNumber);

//        List<CustomerCard> customerCards = remoteCustomerService.queryCardIdByCustomerId(customerId);
//
//        for (com.ktgj.customer.api.domain.CustomerCard customerCard : customerCards) {
//            if (customerCard.getCardId() != null && customerCard.getCardId() != 0){
//                Map<String, Object> map = new HashMap<>();
//                map.put("cardId", customerCard.getCardId());
//                Card card = remoteCustomerService.cardInfo(map);
//                if (card != null){
//                    customerCard.setCardLevel(card.getCardLevel());
//                }
//                else {
//                    throw new Exception("无效的会员卡");
//                }
//            }
//            else {
//                throw new Exception("会员卡没有关联卡id");
//            }
//        }
//
//        customerCards.sort((y, x) -> Double.compare(y.getCardLevel(), x.getCardLevel()));
//        com.ktgj.customer.api.domain.CustomerCard customerCardR = customerCards.get(0);

        OrderVipchannel orderVipchannel = orderVipchannelMapper.queryOrderVipchannel(orderNumber, customerId);

        Long vipchannelid = orderVipchannel.getContractVipchannelid();
        Vipchannel vipchannel = vipchannelService.selectVipchannelById(vipchannelid);
        cashier.setName(vipchannel.getVipchannelName());
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String format = dateFormat.format(orderVipchannel.getArrivalterminalTime());
        cashier.setArrivalviphallTime(format);
        cashier.setPaymentStatus(orderVipchannel.getPaymentStatus());
        cashier.setPayment(orderVipchannel.getPaymentAmount());
        cashier.setAddress(vipchannel.getAddress());
        if (expireTime != null && expireTime > 0) {
//            Map<String, Object> map = new HashMap<>();
//            map.put("businessType", businessType);
            List<PayTypeApi> payTypes = payService.queryPayType(businessType);
            for (PayTypeApi payType : payTypes) {
                PayTypeVo typeVo = new PayTypeVo();
                typeVo.setPayId(payType.getPayId());
                typeVo.setPayName(payType.getPayName());
                typeVo.setPayType(payType.getPayType());
                typeVo.setPayInfo(payType.getPayInfo());
                cashier.getPayType().add(typeVo);
            }
            cashier.setExpiredTime(String.valueOf(expireTime));
        } else {
            cashier.setExpiredTime("订单已超时！");
        }
        String airportId = vipchannel.getAirportCode();
        String airPortName = flightService.queryAirPortName(airportId);
        cashier.setAirPortName(airPortName);
        AirportTerminalApi airportTerminal = new AirportTerminalApi();
        airportTerminal.setTerminalId(vipchannel.getAirportTerminalId());
        R<AirportTerminalApi> terminal = flightService.getAirportTerminal(airportTerminal.getTerminalId());
        cashier.setStartTerminal(terminal.getData().getStartTerminal());
        cashier.setPeopleNumber(orderVipchannel.getPeopleNumber());
        return cashier;
    }

    /**
     * 查询当前用户全部订单
     * */
    @Override
    public OrderVo selectOrderVipchannelByCustomerIdAndOrdervipchannelId(Long customerId, String type) throws Exception {
        OrderVo orderVo = new OrderVo();
        List<TicketOrderVo> ticketOrderVoList = new ArrayList<>();
        List<ViphallOrderVo> viphallOrderVoList = new ArrayList<>();
        List<VipchannelOrederVo> vipchannelOrederVoList = new ArrayList<>();
        List<VipboardingOrderVo> vipboardingOrderVoList = new ArrayList<>();

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        List<TicketOrderApi> ticketOrderApiList = remoteFlightService.checkTheTicketList(customerId);//机票
        if (ticketOrderApiList != null && ticketOrderApiList.size() > 0){
            for (TicketOrderApi ticketOrderApi : ticketOrderApiList) {//机票
                TicketOrderVo ticketOrderVo = new TicketOrderVo();
                ticketOrderVo.setOrderId(ticketOrderApi.getOrderId());
                ticketOrderVo.setIssueTime(ticketOrderApi.getIssueTime());
                ticketOrderVo.setPayAmount(ticketOrderApi.getPayAmount());
                ticketOrderVo.setAirliner(ticketOrderApi.getAirliner());
                ticketOrderVo.setDeparture(ticketOrderApi.getDeparture());
                ticketOrderVo.setDepartureTime(ticketOrderApi.getDepartureTime());
                ticketOrderVo.setArrival(ticketOrderApi.getArrival());
                ticketOrderVo.setFlight(ticketOrderApi.getFlight());
                ticketOrderVo.setOrderType(ticketOrderApi.getOrderType());
                ticketOrderVo.setOrderStatus(ticketOrderApi.getOrderStatus());
                ticketOrderVo.setRoundTripAirliner(ticketOrderApi.getRoundTripAirliner());
                ticketOrderVo.setRoundTripDeparture(ticketOrderApi.getRoundTripDeparture());
                ticketOrderVo.setRoundTripDepartureTime(ticketOrderApi.getRoundTripDepartureTime());
                ticketOrderVo.setRoundTripArrival(ticketOrderApi.getRoundTripArrival());
                ticketOrderVo.setRoundTripFlight(ticketOrderApi.getRoundTripFlight());
                ticketOrderVo.setStopDesc(ticketOrderApi.getStopDesc());
                ticketOrderVoList.add(ticketOrderVo);
            }
            if (ticketOrderVoList != null){
                orderVo.setTicketOrderVoList(ticketOrderVoList);
            }
        }

//        List<CustomerCard> customerCards = remoteCustomerService.queryCardIdByCustomerId(customerId);//会员 卡号
//
//        for (com.ktgj.customer.api.domain.CustomerCard customerCard : customerCards) {
//            if (customerCard.getCardId() != null && customerCard.getCardId() != 0){
//                Map<String, Object> map = new HashMap<>();
//                map.put("cardId", customerCard.getCardId());
//                Card card = remoteCustomerService.cardInfo(map);
//                if (card != null){
//                    customerCard.setCardLevel(card.getCardLevel());
//                }
//                else {
//                    throw new Exception("无效的会员卡");
//                }
//            }
//            else {
//                throw new Exception("会员卡没有关联卡id");
//            }
//        }
//
//        customerCards.sort((y, x) -> Double.compare(y.getCardLevel(), x.getCardLevel()));
//        com.ktgj.customer.api.domain.CustomerCard customerCardR = customerCards.get(0);
//
//        String cardNumber = customerCardR.getCardCode();

//        if (customerCardR != null){
//            List<OrderViphall> orderViphallList = orderViphallMapper.VipRoomOrder(cardNumber);//贵宾厅
//            if (orderViphallList != null){
//                for (OrderViphall orderViphall : orderViphallList) {
//                    if (!orderViphall.getIsDeleted().equals("true")){
//                        Viphall viphall = viphallMapper.viphall(orderViphall.getViphallId());
//                        ViphallOrderVo viphallOrderVo = new ViphallOrderVo();
//                        viphallOrderVo.setOrderviphallId(orderViphall.getOrderviphallId());
//                        viphallOrderVo.setOrderType("贵宾厅/餐厅服务");
//                        viphallOrderVo.setStatus(orderViphall.getStatus());
//                        viphallOrderVo.setViphallName(viphall.getViphallName());
//                        viphallOrderVo.setPaymentAmount(orderViphall.getPaymentAmount());
//                        viphallOrderVo.setArrivalviphallTime(orderViphall.getArrivalviphallTime());
//                        viphallOrderVo.setPeopleNumber(orderViphall.getPeopleNumber());
//                        viphallOrderVo.setAddress(viphall.getAddress());
//                        viphallOrderVo.setCustomerServiceTel(viphall.getCustomerServiceTel());
//                        viphallOrderVo.setCreateTime(format.format(viphall.getCreateTime()));
//                        viphallOrderVoList.add(viphallOrderVo);
//                    }
//                }
//            }
//            else {
//                return null;
//            }

//        }

        if (viphallOrderVoList != null){
            orderVo.setViphallOrderVoList(viphallOrderVoList);
        }

//            List<OrderVipchannel> orderVipchannelList = orderVipchannelMapperw.selectVipchannel(cardNumber);//vip通道
//            if (orderVipchannelList != null){
//                for (OrderVipchannel orderVipchannel : orderVipchannelList) {
//                    if (!orderVipchannel.getIsDeleted().equals("true")){
//                        Vipchannel vipchannelVo = vipchannelMapper.selectVipchannel(orderVipchannel.getVipchannelId());
//                        VipchannelOrederVo vipchannelOrederVo = new VipchannelOrederVo();
//                        vipchannelOrederVo.setOrdervipchannelId(orderVipchannel.getVipchannelId());
//                        vipchannelOrederVo.setVipchannelName(vipchannelVo.getVipchannelName());
//                        vipchannelOrederVo.setAddress(vipchannelVo.getAddress());
//                        vipchannelOrederVo.setFlightNumber(orderVipchannel.getFlightNumber());
//                        vipchannelOrederVo.setCreateTime(orderVipchannel.getCreateTime());
//                        vipchannelOrederVo.setPaymentAmount(orderVipchannel.getPaymentAmount());
//                        vipchannelOrederVo.setPeopleNumber(orderVipchannel.getPeopleNumber());
//                        vipchannelOrederVo.setStatus(orderVipchannel.getStatus());
//                        vipchannelOrederVo.setOrderType("VIP通道");
//                        vipchannelOrederVoList.add(vipchannelOrederVo);
//                    }
//                }
//            }
//            else {
//                return null;
//            }
        if (vipchannelOrederVoList != null){
            orderVo.setVipchannelOrederVoList(vipchannelOrederVoList);
        }

        List<OrderBoarding> orderBoardingList = orderBoardingMapper.selectOrderBoarding(customerId);//vip通道
        if (orderBoardingList != null){
            for (OrderBoarding orderBoarding : orderBoardingList) {
                if (!orderBoarding.getIsDeleted().equals("true")){
                    VipboardingOrderVo vipboardingOrderVo = new VipboardingOrderVo();
                    vipboardingOrderVo.setOrderType("值机");
                    vipboardingOrderVo.setStatus(orderBoarding.getStatus());
                    vipboardingOrderVo.setDepartureCity(orderBoarding.getDepartureCity());
                    vipboardingOrderVo.setDepartureAirportterminalname(orderBoarding.getDepartureAirportterminalname());
                    vipboardingOrderVo.setArrivalAirportterminalname(orderBoarding.getArrivalAirportterminalname());
                    vipboardingOrderVo.setArrivalCity(orderBoarding.getArrivalCity());
                    vipboardingOrderVo.setFlightNumber(orderBoarding.getFlightNumber());
                    vipboardingOrderVo.setTotalBaseprice(orderBoarding.getTotalBaseprice());
                    vipboardingOrderVo.setCreateTime(format.format(orderBoarding.getCreateTime()));
                    vipboardingOrderVoList.add(vipboardingOrderVo);
                }
            }
        }
        else {
            return null;
        }
        if (vipboardingOrderVoList != null){
            orderVo.setVipboardingOrderVoList(vipboardingOrderVoList);
        }

        if (type != null){//条件
            for (int i = 0 ; i < orderVo.getTicketOrderVoList().size() ; i++){
                if (!orderVo.getTicketOrderVoList().get(i).getOrderStatus().equals(type)){
                    orderVo.getTicketOrderVoList().remove(i);
                }
            }
            for (int i = 0 ; i < orderVo.getVipboardingOrderVoList().size() ; i++){
                if (!orderVo.getVipboardingOrderVoList().get(i).getStatus().equals(type)){
                    orderVo.getVipboardingOrderVoList().remove(i);
                }
            }
            for (int i = 0 ; i < orderVo.getVipchannelOrederVoList().size() ; i++){
                if (!orderVo.getVipchannelOrederVoList().get(i).getStatus().equals(type)){
                    orderVo.getVipchannelOrederVoList().remove(i);
                }
            }
            for (int i = 0 ; i < orderVo.getViphallOrderVoList().size() ; i++){
                if (!orderVo.getViphallOrderVoList().get(i).getStatus().equals(type)){
                    orderVo.getViphallOrderVoList().remove(i);
                }
            }
        }
        return orderVo;
    }
    /**
     * 支付成功页面
     * @param ordervipchannelId
     * @return
     */
    @Override
    public Map<String, Object> selectOrderVipchannelNumber(Long ordervipchannelId) {
        OrderVipchannel orderVipchannel = orderVipchannelMapper.selectOrderVipchannelNumber(ordervipchannelId);
        Map<String, Object> map = new HashMap<>();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = dateFormat.format(orderVipchannel.getCreateTime());
        map.put("orderNumber", orderVipchannel.getOrderNumber());
        map.put("createTime", format);
        map.put("memberaccountpaymentmethod", orderVipchannel.getMemberaccountpaymentmethod());
        return map;
    }

    @Override
    public Map<String, Object> queryVipchannelTrip(Long ordervipchannelId) {
        List<OrderVipchannel> orderVipchannelList = orderVipchannelMapper.queryVipchannelTrip(ordervipchannelId);
        Map<String, Object> map = new HashMap<>();
        StringBuilder builder;
        for (OrderVipchannel orderVipchannel : orderVipchannelList) {
            map.put("peopleNumber", orderVipchannel.getPeopleNumber());
            for (Vipchannel vipchannel : orderVipchannel.getVipchannels()) {
                map.put("vipchannelId", vipchannel.getVipchannelId());
                map.put("vipchannelName", vipchannel.getVipchannelName());
                map.put("address", vipchannel.getAddress());
                Long businessBeginHour = vipchannel.getBusinessBeginHour();
                if (businessBeginHour != null) {
                    builder = new StringBuilder(businessBeginHour.toString());
                    map.put("businessBeginHour", builder.insert(builder.length() - 2, ":"));
                }
                Long businessEndHour = vipchannel.getBusinessEndHour();
                if (businessEndHour != null) {
                    builder = new StringBuilder(businessEndHour.toString());
                    map.put("businessEndHour", builder.insert(builder.length() - 2, ":"));
                }
                map.put("imgUrl", "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fp7.itc.cn%2Fimages01%2F20201103%2Ffbb33b469ab64308825015e486d2f1ed.jpeg&refer=http%3A%2F%2Fp7.itc.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1640238516&t=a0bbeffb6af656bb16b02b16ce68933c");
            }
        }
        return map;
    }

    @Override
    public Map<String, Object>queryVipchannelTripByOrderNumber(Long orderNumber) {
        List<OrderVipchannel> orderVipchannelList = orderVipchannelMapper.queryVipchannelTripByOrderNumber(orderNumber);
        Map<String, Object> map = new HashMap<>();
        StringBuilder builder;
        for (OrderVipchannel orderVipchannel : orderVipchannelList) {
            map.put("peopleNumber", orderVipchannel.getPeopleNumber());
            for (Vipchannel vipchannel : orderVipchannel.getVipchannels()) {
                map.put("vipchannelId", vipchannel.getVipchannelId());
                map.put("vipchannelName", vipchannel.getVipchannelName());
                map.put("address", vipchannel.getAddress());
                Long businessBeginHour = vipchannel.getBusinessBeginHour();
                if (businessBeginHour != null) {
                    builder = new StringBuilder(businessBeginHour.toString());
                    map.put("businessBeginHour", builder.insert(builder.length() - 2, ":"));
                }
                Long businessEndHour = vipchannel.getBusinessEndHour();
                if (businessEndHour != null) {
                    builder = new StringBuilder(businessEndHour.toString());
                    map.put("businessEndHour", builder.insert(builder.length() - 2, ":"));
                }
                map.put("imgUrl", "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fp7.itc.cn%2Fimages01%2F20201103%2Ffbb33b469ab64308825015e486d2f1ed.jpeg&refer=http%3A%2F%2Fp7.itc.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1640238516&t=a0bbeffb6af656bb16b02b16ce68933c");
            }
        }
        return map;
    }

    @Override
    public Map<String, Object> selectItinerary(Long ordervipchannelId) {
        OrderVipchannel orderVipchannel = orderVipchannelMapper.selectItinerary(ordervipchannelId);
        Map<String, Object> map = new HashMap<>();
        StringBuilder builder;
        map.put("peopleNumber", orderVipchannel.getPeopleNumber());
        for (Vipchannel vipchannel : orderVipchannel.getVipchannels()) {
            map.put("vipchannelId", vipchannel.getVipchannelId());
            map.put("vipchannelName", vipchannel.getVipchannelName());
            map.put("address", vipchannel.getAddress());
            Long businessBeginHour = vipchannel.getBusinessBeginHour();
            if (businessBeginHour != null) {
                builder = new StringBuilder(businessBeginHour.toString());
                map.put("businessBeginHour", builder.insert(builder.length() - 2, ":"));
            }
            Long businessEndHour = vipchannel.getBusinessEndHour();
            if (businessEndHour != null) {
                builder = new StringBuilder(businessEndHour.toString());
                map.put("businessEndHour", builder.insert(builder.length() - 2, ":"));
            }
            map.put("freeMember", vipchannel.getFreeMember());
            map.put("imgUrl", "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fp7.itc.cn%2Fimages01%2F20201103%2Ffbb33b469ab64308825015e486d2f1ed.jpeg&refer=http%3A%2F%2Fp7.itc.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1640238516&t=a0bbeffb6af656bb16b02b16ce68933c");
            for (ContractLinkVipchannelPrice linkVipchannelPrice : vipchannel.getLstVipchannelPrice()) {
                map.put("retailPrice", linkVipchannelPrice.getRecommendedRetailprice());
                map.put("salePrice", linkVipchannelPrice.getSalePrice());
            }
        }
        return map;
    }

    @Override
    public List<Vipchannel> selectOrderVipchannelByStatus(Long customerId) {
        return orderVipchannelMapper.selectOrderVipchannelByStatus(customerId);
    }

    @Override
    public OrderVipchannel queryItinerary(Long customerId) {
        OrderVipchannel vipchannel = orderVipchannelMapper.queryItinerary(customerId, CustomerConstants.waitPay.getValue());
        return vipchannel;
    }

    //微信小程序支付
    @Override
    public Map<String, Object> wechatPay(String openId, Long orderNumber, BigDecimal amount, String type) throws Exception {
        OrderVipchannel vipchannel = orderVipchannelMapper.queryByOrderNumber(orderNumber);

        if (amount != null && amount.compareTo(new BigDecimal(0)) > 0){
            vipchannel.setPaymentAmount(vipchannel.getPaymentAmount().subtract(amount));
            vipchannel.setDepositAmount(amount);
            int i = orderVipchannelMapper.updateOrderVipchannel(vipchannel);
            if (i <= 0){
                throw new Exception("修改订单预留款失败");
            }

            DepositBusinessApi depositBusiness = new DepositBusinessApi();
            depositBusiness.setRemark("vip通道微信小程序支付");
            depositBusiness.setState("false");
            depositBusiness.setBusinessType(EmHelper.businessType.VIPAisle.getValue());
            depositBusiness.setOperatorId(1L);
            depositBusiness.setOrdersourceid(type);
            depositBusiness.setCustomerId(vipchannel.getCustomerId());
            depositBusiness.setDetailamount(amount);
            depositBusiness.setChangetype(2l);
            depositBusiness.setLockState("false");
            depositBusiness.setOrderId(Long.valueOf(vipchannel.getOrderNumber()));
            depositBusiness.setDrawState("false");
            Long aLong = remoteCustomerService.addReserveMoney(depositBusiness);
            if (aLong <= 0){
                throw new Exception("添加预留款记录失败");
            }

            Map<String, Object> map = new HashMap<>();
            map.put("id", aLong);
            map.put("remark", "vip通道微信小程序支付");
            boolean b = remoteCustomerService.manageAudit(map);
            if (!b){
                throw new Exception("审核失败，请进行人工审核");
            }
        }

        String notifyUrl = "/service/callback/vipChannelWechatPayCallback";
        R<Map> wechatJSAPIPayParam = payService.getWechatJSAPIPayParam(openId, vipchannel.getPaymentAmount(), String.valueOf(orderNumber), notifyUrl);
        if (wechatJSAPIPayParam.getCode() == 200) {
            vipchannel.setPayType(BusinessTypeConstants.Wechat.getValue());
            orderVipchannelMapper.updateOrderVipchannel(vipchannel);
            return wechatJSAPIPayParam.getData();
        }
        return null;
    }

    @Override
    public int updateByOrderNumber(OrderVipchannel orderVipchannel) {
        return orderVipchannelMapper.updateByOrderNumber(orderVipchannel);
    }

    /**
     * 查询vip通道订单列表
     * */
    @Override
    public List<OrderVipchannel> selectOrderVipchannel(Long customerId, String type) throws Exception {
//        List<CustomerCard> customerCards = remoteCustomerService.queryCardIdByCustomerId(customerId);//会员 卡号
//
//        for (com.ktgj.customer.api.domain.CustomerCard customerCard : customerCards) {
//            if (customerCard.getCardId() != null && customerCard.getCardId() != 0){
//                Map<String, Object> map = new HashMap<>();
//                map.put("cardId", customerCard.getCardId());
//                Card card = remoteCustomerService.cardInfo(map);
//                if (card != null){
//                    customerCard.setCardLevel(card.getCardLevel());
//                }
//                else {
//                    throw new Exception("无效的会员卡");
//                }
//            }
//            else {
//                throw new Exception("会员卡没有关联卡id");
//            }
//        }
//
//        customerCards.sort((y, x) -> Double.compare(y.getCardLevel(), x.getCardLevel()));
//        com.ktgj.customer.api.domain.CustomerCard customerCardR = customerCards.get(0);
//
//        String cardNumber = customerCardR.getCardCode();
        List<OrderVipchannel> orderVipchannelList = orderVipchannelMapper.selectVipchannel(customerId, type);//vip通道

        for (OrderVipchannel orderVipchannel : orderVipchannelList) {
            Vipchannel vipchannel = vipchannelMapper.selectVipchannel(orderVipchannel.getVipchannelId());
            vipchannel.setAirportName(flightService.queryAirPortName(vipchannel.getAirportCode()));
            orderVipchannel.setVipchannel(vipchannel);
        }

        return orderVipchannelList;
    }

    /**
     * 条件查询vip通道订单列表
     * */
    @Override
    public List<OrderVipchannel> selectVipchannelOrederListVo(VipchannelOrederParameterVo vipchannelOrederParameterVo) {
        List<OrderVipchannel> orderVipchannelList = orderVipchannelMapper.orderVipchannelList(vipchannelOrederParameterVo);
        if (orderVipchannelList.size() > 0){
            for (OrderVipchannel orderVipchannel : orderVipchannelList) {
                CustomerIdVoApi customerIdVoApi = new CustomerIdVoApi();
                customerIdVoApi.setCustomerId(orderVipchannel.getCustomerId());
                customerIdVoApi = remoteCustomerService.customerIdApi(customerIdVoApi);
                if (customerIdVoApi != null){
                    orderVipchannel.setCustomerName(customerIdVoApi.getCustomerName());
                }

                if (orderVipchannel.getDepartmentId() != null && orderVipchannel.getDepartmentId() != 0){
                    Map<String, Object> map = new HashMap<>();
                    map.put("id", orderVipchannel.getDepartmentId());
                    Department department = remoteCustomerService.deptName(map);
                    if (department != null){
                        orderVipchannel.setDeptName(department.getDeptName());
                    }
                }
            }
        }
        return orderVipchannelList;
    }

    /**
     * vip通道订单详细
     * */
    @Override
    public OrderViphallDetailsVo selectVipchannelOrederListDetailsVo(Long ordervipchannelId, String orderNum) throws Exception {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

        OrderVipchannel orderVipchannel;
        if (ordervipchannelId != null){
            orderVipchannel = orderVipchannelMapper.selectVipchannelOrederListDetailsVo(ordervipchannelId);
        }
        else {
            orderVipchannel = orderVipchannelMapper.vipchannelOrederListDetailsVo(orderNum);
        }

        if (orderVipchannel != null){
            OrderViphallDetailsVo orderViphallDetailsVo = new OrderViphallDetailsVo();
            orderViphallDetailsVo.setOrderId(orderVipchannel.getOrdervipchannelId());

            if (orderVipchannel.getCustomerId() != null && orderVipchannel.getCustomerId() != 0){
                Customer customer = remoteCustomerService.getCustomerInfo(orderVipchannel.getCustomerId()).getData();
                orderViphallDetailsVo.setCustomerName(customer.getCustomerName());
                orderViphallDetailsVo.setCustomerPhone(customer.getContractPhoneOne());
            }
            orderViphallDetailsVo.setCustomerCard(orderVipchannel.getCardNumber());
            if (orderVipchannel.getCreateTime() != null){
                orderViphallDetailsVo.setCreateTime(format.format(orderVipchannel.getCreateTime()));
            }
            orderViphallDetailsVo.setSourceCompanyId(orderVipchannel.getSourceAppId());
            orderViphallDetailsVo.setSourceAppId(orderVipchannel.getSourcecompanyId());
            orderViphallDetailsVo.setContactName(orderVipchannel.getContactName());
            orderViphallDetailsVo.setContactPhone(orderVipchannel.getContactPhone());
            if (orderVipchannel.getOrderNumber() != null){
                orderViphallDetailsVo.setOrderNum(orderVipchannel.getOrderNumber().toString());
            }

            orderViphallDetailsVo.setAdoptPeopleNumber(orderVipchannel.getPeopleNumber());
            orderViphallDetailsVo.setCityName(orderVipchannel.getDepartureCity());
            orderViphallDetailsVo.setSetOffAirport(orderVipchannel.getSetOffAirport());
            orderViphallDetailsVo.setDepartuprairportTerminalName(orderVipchannel.getDepartureAirportterminalname());
            orderViphallDetailsVo.setFlightNumber(orderVipchannel.getFlightNumber());
            orderViphallDetailsVo.setDeparturesCity(orderVipchannel.getDepartureCity());
            orderViphallDetailsVo.setArrivalCity(orderVipchannel.getArrivalCity());
            if (orderVipchannel.getDepartureTime() != null){
                orderViphallDetailsVo.setDepartureTime(format.format(orderVipchannel.getDepartureTime()));
            }
            if (orderVipchannel.getArrivalTime() != null){
                orderViphallDetailsVo.setArrivalTime(format.format(orderVipchannel.getArrivalTime()));
            }
            if (orderVipchannel.getTotalRecommendedretailprice() != null){
                orderViphallDetailsVo.setTotalReCommenedRetatlPrice(orderVipchannel.getTotalRecommendedretailprice().toString());
            }
            if (orderVipchannel.getTotalSaleprice() != null){
                orderViphallDetailsVo.setTotalsalePrice(orderVipchannel.getTotalSaleprice().toString());
            }

            orderViphallDetailsVo.setPaymentStatus(orderVipchannel.getPaymentStatus());
            orderViphallDetailsVo.setPaymentType(orderVipchannel.getMemberaccountpaymentmethod());
            if (orderVipchannel.getPaymentAmount() != null){
                orderViphallDetailsVo.setPaymentAmount(orderVipchannel.getPaymentAmount().toString());
            }
            if (orderVipchannel.getVoucherAmount() != null){
                orderViphallDetailsVo.setVoucherAmount(orderVipchannel.getVoucherAmount().toString());
            }
            if (orderVipchannel.getPoints() != null){
                orderViphallDetailsVo.setPoints(orderVipchannel.getPoints().toString());
            }
            if (orderVipchannel.getDepositAmount() != null){
                orderViphallDetailsVo.setDepositAmount(orderVipchannel.getDepositAmount().toString());
            }
            List<OrderOperationlogVo> operationRecord = new ArrayList<>();

            if (orderVipchannel.getOrderNumber() != null){
                List<OrderOperationlog> orderOperationlogs = orderOperationlogMapper.orderOperationlogList(Long.valueOf(orderVipchannel.getOrderNumber()));
                if (orderOperationlogs != null && orderOperationlogs.size() > 0){
                    for (OrderOperationlog orderOperationlog : orderOperationlogs) {
                        OrderOperationlogVo orderOperationlogVo = new OrderOperationlogVo();
                        orderOperationlogVo.setOperationId(orderOperationlog.getOperationId());
                        orderOperationlogVo.setOperationName(orderOperationlog.getOperationName());
                        orderOperationlogVo.setOperatorName(orderOperationlog.getOperatorName());
                        if (orderOperationlog.getCreateTime() != null){
                            orderOperationlogVo.setCreateTime(format.format(orderOperationlog.getCreateTime()));
                        }
                        orderOperationlogVo.setMemo(orderOperationlog.getMemo());
                        operationRecord.add(orderOperationlogVo);
                    }
                }
            }
            orderViphallDetailsVo.setOperationRecord(operationRecord);

            List<OrderItemViphallVo> orderItemViphallVoList = new ArrayList<>();
            List<OrderitemVipchannel> orderitemVipchannels = orderitemVipchannelMapper.selectOrderitemVipchannelDetails(orderVipchannel.getVipchannelId());
            if (orderitemVipchannels != null && orderitemVipchannels.size() > 0){
                for (OrderitemVipchannel orderitemVipchannel : orderitemVipchannels) {
                    OrderItemViphallVo orderItemViphallVo = new OrderItemViphallVo();
                    orderItemViphallVo.setOrderItemBoardingId(orderVipchannel.getOrdervipchannelId());
                    orderItemViphallVo.setGuestName(orderitemVipchannel.getGuestName());
                    orderItemViphallVo.setIdNumber(orderitemVipchannel.getIdNumber());
                    orderItemViphallVoList.add(orderItemViphallVo);
                }
            }
            orderViphallDetailsVo.setOrderItemViphallVoList(orderItemViphallVoList);
            return orderViphallDetailsVo;
        }
        else {
            throw new Exception("未查到数据");
        }
    }


    /**
     * vip通道行程订单
     * */
    @Override
    public JourneyOrderViphallVo journeyOrderVipchannelVo(Long ordervipchannelId) {
        OrderVipchannel orderVipchannel = orderVipchannelMapper.selectVipchannelOrederListDetailsVo(ordervipchannelId);
        JourneyOrderViphallVo journeyOrderViphallVo = new JourneyOrderViphallVo();
        journeyOrderViphallVo.setOrderId(orderVipchannel.getOrdervipchannelId().toString());
        journeyOrderViphallVo.setBusinessType(EmHelper.businessType.VIPAisle.getValue());
        journeyOrderViphallVo.setActualPriceTotal(orderVipchannel.getPaymentAmount().toString());
        if (orderVipchannel.getPaymentStatus().equals(EmHelper.OrderStatus.Unpaid.getValue())){
            journeyOrderViphallVo.setOrderStatusID("未支付");
        }
        else if (orderVipchannel.getPaymentStatus().equals(CustomerConstants.payd.getValue())){
            if (orderVipchannel.getStatus().equals(TrainOrderEnum.cancellation.getValue())){
                journeyOrderViphallVo.setOrderStatusID("已退款");
            }
            else if (orderVipchannel.getStatus().equals(CustomerConstants.payd.getValue())){
                journeyOrderViphallVo.setOrderStatusID("已支付");
            }
            else {
                journeyOrderViphallVo.setOrderStatusID(orderVipchannel.getStatus());
            }
        }
        else if (orderVipchannel.getPaymentStatus().equals(CustomerConstants.cancelPay.getValue())){
            journeyOrderViphallVo.setOrderStatusID("取消支付");
        }
        journeyOrderViphallVo.setContactPhone(orderVipchannel.getContactPhone());
        journeyOrderViphallVo.setPeopleNumber(orderVipchannel.getPeopleNumber().toString());
        Vipchannel vipchannel = vipchannelMapper.selectVipchannel(orderVipchannel.getVipchannelId());
        if (vipchannel != null){
            journeyOrderViphallVo.setViphallName(vipchannel.getVipchannelName());
            journeyOrderViphallVo.setAddress(vipchannel.getAddress());
        }
        return journeyOrderViphallVo;
    }

    /**
     * VIP通道退单
     * */
    @Override
    public int chargebackVipchannel(Long ordervipchannelId) {
        int i = orderVipchannelMapper.chargebackVipchannel(ordervipchannelId);
        return i;
    }
    /**
     * 订单号查数据
     * */
    @Override
    public OrderViphallDetailsVo queryByOrderNumber(Long orderNumber) throws Exception{
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        OrderVipchannel orderVipchannel = orderVipchannelMapper.queryByOrderNumber(orderNumber);
        if (orderVipchannel != null){
            OrderViphallDetailsVo orderViphallDetailsVo = new OrderViphallDetailsVo();
            orderViphallDetailsVo.setOrderId(orderVipchannel.getOrdervipchannelId());

            if (orderVipchannel.getCustomerId() != null && orderVipchannel.getCustomerId() != 0){
                Customer customer = remoteCustomerService.getCustomerInfo(orderVipchannel.getCustomerId()).getData();
                orderViphallDetailsVo.setCustomerName(customer.getCustomerName());
                orderViphallDetailsVo.setCustomerPhone(customer.getContractPhoneOne());
            }
            orderViphallDetailsVo.setCustomerCard(orderVipchannel.getCardNumber());
            if (orderVipchannel.getCreateTime() != null){
                orderViphallDetailsVo.setCreateTime(format.format(orderVipchannel.getCreateTime()));
            }
            orderViphallDetailsVo.setSourceCompanyId(orderVipchannel.getSourceAppId());
            orderViphallDetailsVo.setSourceAppId(orderVipchannel.getSourcecompanyId());
            orderViphallDetailsVo.setContactName(orderVipchannel.getContactName());
            orderViphallDetailsVo.setContactPhone(orderVipchannel.getContactPhone());
            if (orderVipchannel.getOrderNumber() != null){
                orderViphallDetailsVo.setOrderNum(orderVipchannel.getOrderNumber().toString());
            }

            orderViphallDetailsVo.setAdoptPeopleNumber(orderVipchannel.getPeopleNumber());
            orderViphallDetailsVo.setCityName(orderVipchannel.getDepartureCity());
            orderViphallDetailsVo.setSetOffAirport(orderVipchannel.getSetOffAirport());
            orderViphallDetailsVo.setDepartuprairportTerminalName(orderVipchannel.getDepartureAirportterminalname());
            orderViphallDetailsVo.setFlightNumber(orderVipchannel.getFlightNumber());
            orderViphallDetailsVo.setDeparturesCity(orderVipchannel.getDepartureCity());
            orderViphallDetailsVo.setArrivalCity(orderVipchannel.getArrivalCity());
            if (orderVipchannel.getDepartureTime() != null){
                orderViphallDetailsVo.setDepartureTime(format.format(orderVipchannel.getDepartureTime()));
            }
            if (orderVipchannel.getArrivalTime() != null){
                orderViphallDetailsVo.setArrivalTime(format.format(orderVipchannel.getArrivalTime()));
            }
            if (orderVipchannel.getTotalRecommendedretailprice() != null){
                orderViphallDetailsVo.setTotalReCommenedRetatlPrice(orderVipchannel.getTotalRecommendedretailprice().toString());
            }
            if (orderVipchannel.getTotalSaleprice() != null){
                orderViphallDetailsVo.setTotalsalePrice(orderVipchannel.getTotalSaleprice().toString());
            }

            orderViphallDetailsVo.setPaymentStatus(orderVipchannel.getPaymentStatus());
            orderViphallDetailsVo.setPaymentType(orderVipchannel.getMemberaccountpaymentmethod());
            if (orderVipchannel.getPaymentAmount() != null){
                orderViphallDetailsVo.setPaymentAmount(orderVipchannel.getPaymentAmount().toString());
            }
            if (orderVipchannel.getVoucherAmount() != null){
                orderViphallDetailsVo.setVoucherAmount(orderVipchannel.getVoucherAmount().toString());
            }
            if (orderVipchannel.getPoints() != null){
                orderViphallDetailsVo.setPoints(orderVipchannel.getPoints().toString());
            }
            if (orderVipchannel.getDepositAmount() != null){
                orderViphallDetailsVo.setDepositAmount(orderVipchannel.getDepositAmount().toString());
            }
            List<OrderOperationlogVo> operationRecord = new ArrayList<>();

            if (orderVipchannel.getOrderNumber() != null){
                List<OrderOperationlog> orderOperationlogs = orderOperationlogMapper.orderOperationlogList(Long.valueOf(orderVipchannel.getOrderNumber()));
                if (orderOperationlogs != null && orderOperationlogs.size() > 0){
                    for (OrderOperationlog orderOperationlog : orderOperationlogs) {
                        OrderOperationlogVo orderOperationlogVo = new OrderOperationlogVo();
                        orderOperationlogVo.setOperationId(orderOperationlog.getOperationId());
                        orderOperationlogVo.setOperationName(orderOperationlog.getOperationName());
                        orderOperationlogVo.setOperatorName(orderOperationlog.getOperatorName());
                        if (orderOperationlog.getCreateTime() != null){
                            orderOperationlogVo.setCreateTime(format.format(orderOperationlog.getCreateTime()));
                        }
                        orderOperationlogVo.setMemo(orderOperationlog.getMemo());
                        operationRecord.add(orderOperationlogVo);
                    }
                }
            }
            orderViphallDetailsVo.setOperationRecord(operationRecord);

            List<OrderItemViphallVo> orderItemViphallVoList = new ArrayList<>();
            List<OrderitemVipchannel> orderitemVipchannels = orderitemVipchannelMapper.selectOrderitemVipchannelDetails(orderVipchannel.getVipchannelId());
            if (orderitemVipchannels != null && orderitemVipchannels.size() > 0){
                for (OrderitemVipchannel orderitemVipchannel : orderitemVipchannels) {
                    OrderItemViphallVo orderItemViphallVo = new OrderItemViphallVo();
                    orderItemViphallVo.setOrderItemBoardingId(orderVipchannel.getOrdervipchannelId());
                    orderItemViphallVo.setGuestName(orderitemVipchannel.getGuestName());
                    orderItemViphallVo.setIdNumber(orderitemVipchannel.getIdNumber());
                    orderItemViphallVoList.add(orderItemViphallVo);
                }
            }
            orderViphallDetailsVo.setOrderItemViphallVoList(orderItemViphallVoList);
            return orderViphallDetailsVo;
        }
        else {
            throw new Exception("未查到数据");
        }
    }

    /**
     * 订单号获取vip通道订单信息
     * */
    @Override
    public OrderVipchannel orderVipchannel(String orderNum) {
        OrderVipchannel orderVipchannel = orderVipchannelMapper.vipchannelOrederListDetailsVo(orderNum);
        return orderVipchannel;
    }
}
